/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mygame.Attacks;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import java.io.IOException;

/**
 *
 * @author Paulo
 */
public class ProjectileControl extends AbstractControl{
    //Any local variables should be encapsulated by getters/setters so they
    //appear in the SDK properties window and can be edited.
    //Right-click a local variable to encapsulate it with getters and setters.

    protected Vector3f velocity = new Vector3f();
    protected float lifetime = 5;
    protected float time = 0;
    
    public Vector3f getVelocity() {
        return velocity.clone();
    }
    
    public void setVelocity(Vector3f velocity) {
        this.velocity = velocity.clone();
    }
    
    public float getLifetime() {
        return this.lifetime;
    }
    
    public void setLifetime(float lifetime) {
        this.lifetime = lifetime;
    }
    
    @Override
    protected void controlUpdate(float tpf) {
        time += tpf;
        
        spatial.move(velocity.mult(tpf));
        
        if (time >= lifetime) {
            spatial.removeFromParent();
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    @Override
    public Control cloneForSpatial(Spatial spatial) {
        ProjectileControl control = new ProjectileControl();
        control.setVelocity(getVelocity());
        return control;
    }

    @Override
    public void read(JmeImporter im) throws IOException {
        super.read(im);
        InputCapsule in = im.getCapsule(this);
        this.velocity = (Vector3f) in.readSavable("velocity", Vector3f.ZERO.clone());
        this.lifetime = in.readFloat("lifetime", 0);
        this.time = in.readFloat("time", 0);
    }

    @Override
    public void write(JmeExporter ex) throws IOException {
        super.write(ex);
        OutputCapsule out = ex.getCapsule(this);
        out.write(velocity, "velocity", Vector3f.ZERO.clone());
        out.write(lifetime, "lifetime", 0);
        out.write(time, "time", 0);
    }


}
